<?php

namespace App\model;

use App\Model\SkuModel;
use App\Model\ShopGoodModel;
use App\Model\AddShopCost;
use App\Model\ConfigModel;
use Illuminate\Database\Eloquent\Model;

//录入商品
class AddShop extends Model
{

    protected $table = 'add_shop';

    protected $guarded = [];

	function getTnameAttribute($value)
	{
		//分类
		$type_name = '';
		$type = AddShopType::where('gid',$this->id)->select('tid')->get();
		$tid = '';
		foreach($type as $key =>$val){
			$tid .= $val['tid'].",";
		}
		if($tid != ''){
			$tname = '';
			$tids = explode(',',trim($tid,','));

			foreach($tids as $j=>$i){
				$tname .= AddType::where('id',$i)->value('name').",";
			}

			$type_name = $tname;
		}
		return $type_name;
	}

	function getBnameAttribute($value)
	{
		$bname = '';

		$brand = AddBrand::where('id',$this->bid)->value('name_cny');
		if($brand){
			$bname = $brand;
		}
		return $bname;
	}

	function getPriceAttribute($value)
	{
		$merchant = AddShopCost::where('gid',$this->id)->orderby('cost','asc')->get();
		if(count($merchant) >= 3){
			$price = AddShopCost::where('gid',$this->id)->orderby('cost','asc')->offset(2)->take(1)->value('cost');
		}else{
			$price = 0;
			if(AddShopCost::where('gid',$this->id)->orderby('cost','desc')->value('cost')){
				$price = AddShopCost::where('gid',$this->id)->orderby('cost','desc')->value('cost');
			}
		}
		return $price;
	}

	function getSellPriceAttribute($value){
		if($this->is_chage == 1){
			$sell_price = $this->new_sell_price;
		}else{
			$sell_price = ceil($this->price/ConfigModel::getValue('sell_ratio')).".00";
		}
		return $sell_price;
	}

	function getChargeAttribute($value)
	{
		$mer_num = $this->sell_price-$this->price;
		return $mer_num;
	}

	function getMerNumAttribute($value)
	{
		$mer_num = AddShopCost::where('gid',$this->id)->count();
		return $mer_num;
	}

	function getAttrIdAttribute($value)
	{
		$attrid = SkuModel::where('store_id',$this->id)->where('state',1)->value('gid');
		if($attrid > 0){
			$attrid = $attrid;
		}else{
			$attrid = ShopGoodModel::where('gid',$this->id)->value('id');
		}
		return $attrid;
	}



}